//
//  BKNoteListCell.m
//  lg_zhinengbeikao_english
//
//  Created by 盛超 on 2019/1/8.
//  Copyright © 2019年 zhukai. All rights reserved.
//

#import "BKNoteListCell.h"
#import "XLPhotoBrowser.h"

@interface BKNoteListCell ()<XLPhotoBrowserDatasource>

@property (nonatomic,strong) UIView *bgView;
@property (nonatomic,strong) UILabel *fromInfoLabel;
@property (nonatomic,strong) UILabel *contentInfoLabel;
@property (nonatomic,strong) UILabel *timeLabel;
//@property (nonatomic,strong) UIView *iconView;
//侧滑删除按钮
@property (nonatomic,strong) UIButton *deleteBtn;
//图片
@property (nonatomic,strong) NSMutableArray *images;

@end

@implementation BKNoteListCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.backgroundColor = rgba(245, 245, 245, 1);
        
        [self setupUI];
        
        //添加左滑和右滑手势
        UISwipeGestureRecognizer *leftSwipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(leftSwipe:)];
        leftSwipeGesture.direction = UISwipeGestureRecognizerDirectionLeft;
        [self.contentView addGestureRecognizer:leftSwipeGesture];

        UISwipeGestureRecognizer *rightSwipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(rightSwipe:)];
        rightSwipeGesture.direction = UISwipeGestureRecognizerDirectionRight;
        [self.contentView addGestureRecognizer:rightSwipeGesture];
        
    }
    
    return self;
    
}

- (void)setupUI {
    
    _bgView = [UIView new];
    _bgView.userInteractionEnabled = YES;
    _bgView.backgroundColor = [UIColor whiteColor];
    _bgView.layer.cornerRadius = 10;
    _bgView.layer.masksToBounds = YES;
    [self addSubview:_bgView];
    
    _fromInfoLabel = [[UILabel alloc] init];
    _fromInfoLabel.font = FONT(13);
    _fromInfoLabel.text = @"笔记试题";
    [self.bgView addSubview:_fromInfoLabel];
    [_fromInfoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(10);
        make.right.mas_offset(-10);
        make.top.equalTo(self.bgView).offset(10);
    }];
    
    UIImageView  *arrowimgView = [[UIImageView alloc] init];
    arrowimgView.backgroundColor = [UIColor redColor];
    [self.bgView addSubview:arrowimgView];
    [arrowimgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.bgView).offset(-10);
        make.centerY.equalTo(self.fromInfoLabel.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(8, 14));
    }];
    
    _contentInfoLabel = [[UILabel alloc] init];
    _contentInfoLabel.font = FONT(13);
    _contentInfoLabel.text = @"笔记内容";
    [self.bgView addSubview:_contentInfoLabel];
    [_contentInfoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(10);
        make.right.mas_offset(-10);
        make.top.equalTo(self.fromInfoLabel.mas_bottom).offset(10);
    }];
    
    _timeLabel = [[UILabel alloc] init];
    _timeLabel.font = FONT(11);
    _timeLabel.numberOfLines = 0;
    _timeLabel.textColor = rgba(181, 181, 181, 1);
    _timeLabel.text = @"最近编辑:2018.01.08 18:13";
    [self.bgView addSubview:_timeLabel];
    [_timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(10);
        make.right.mas_offset(-10);
        make.top.equalTo(self.contentInfoLabel.mas_bottom).offset(10);
        make.bottom.equalTo(self.bgView).offset(-10-10-W(40));
    }];
    
    //图片
    NSInteger itemCount = 3;
    UIView *lastSpaceView = nil;
    for (NSInteger i = 0; i<itemCount; i++) {

        UIButton *imgBtn = [UIButton new];
        imgBtn.userInteractionEnabled = YES;

        imgBtn.tag = 333+i;
        imgBtn.layer.cornerRadius = 5;
        imgBtn.layer.masksToBounds = YES;
        [self addSubview:imgBtn];

        [imgBtn mas_makeConstraints:^(MASConstraintMaker *make) {

            make.bottom.equalTo(self).offset(-20);
            make.height.width.mas_equalTo(W(40));
            if (lastSpaceView) {

                make.left.equalTo(lastSpaceView.mas_right).mas_equalTo(10);

            } else {

                make.left.equalTo(self.mas_left).mas_equalTo(25);

            }
        }];
        lastSpaceView = imgBtn;
        [imgBtn addTarget:self action:@selector(clickImage:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    [self insertSubview:_bgView atIndex:0];
    [_bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        //make.edges.mas_equalTo(UIEdgeInsetsMake(5, 10, 5, 10));
        make.left.equalTo(self).offset(15);
        make.right.equalTo(self).offset(-15);
        make.top.equalTo(self).offset(10);
        make.bottom.equalTo(self).offset(-10);
    }];
    
    //删除按钮
    self.deleteBtn = [[UIButton alloc] init];
    self.deleteBtn.backgroundColor = rgba(244, 78, 78, 1);
    self.deleteBtn.titleLabel.textColor = [UIColor whiteColor];
    [self.deleteBtn setTitle:@"删除" forState:UIControlStateNormal];
    [self addSubview:self.deleteBtn];
    [self.deleteBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(10);
        make.bottom.equalTo(self).offset(-10);
        make.left.equalTo(self.mas_right).offset(-20);
        make.width.mas_equalTo(50);
    }];
    self.deleteBtn.alpha = 0;
    [self.deleteBtn addTarget:self action:@selector(deleteAction:) forControlEvents:UIControlEventTouchUpInside];
    
    //调用layoutIfNeeded后就可以在Masonry布局后获取Frame
    [self layoutIfNeeded];
    
    CGFloat height = 10*6+13*2+10+W(40);
    [self addToView:self.deleteBtn Rect:CGRectMake(0,0,50,height) RounedCorner:UIRectCornerTopRight|UIRectCornerBottomRight withRadii:CGSizeMake(10, 10)];

}

//设置哪几个边是圆角
- (void)addToView:(UIView*)roundView Rect:(CGRect)rect RounedCorner:(UIRectCorner)corner withRadii:(CGSize)radii {
    
    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:rect byRoundingCorners:corner cornerRadii:radii];
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    shapeLayer.frame = rect;
    shapeLayer.path = maskPath.CGPath;
    roundView.layer.mask = shapeLayer;
}


#pragma mark - 手势事件
- (void)leftSwipe:(UISwipeGestureRecognizer*)sender {
    NSLog(@"左滑手势");
    self.deleteBtn.alpha = 1;
    
    UIButton *leftBtn = [self viewWithTag:333];
    
    [UIView animateWithDuration:0.3 animations:^{
        [self.bgView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(-35);
            make.right.equalTo(self).offset(-65);
        }];
        [self.deleteBtn mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.mas_right).offset(-70);
        }];
        
        [leftBtn mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.mas_left).mas_equalTo(-25);
        }];
       
        [self layoutIfNeeded];
    }];
    
     self.listBlock(NO,YES,_indexPath);
}

- (void)rightSwipe:(UISwipeGestureRecognizer*)sender {
    
    UIButton *leftBtn = [self viewWithTag:333];
    
    [UIView animateWithDuration:0.3 animations:^{
        [self.bgView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(15);
            make.right.equalTo(self).offset(-15);
        }];
        [self.deleteBtn mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.mas_right).offset(-30);
        }];
        self.deleteBtn.alpha = 0;
        
        [leftBtn mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.mas_left).mas_equalTo(25);
        }];
        
        [self layoutIfNeeded];
    }];
    
    self.listBlock(NO,NO,_indexPath);
    
}

- (void)deleteAction:(id)sender {
    
    self.listBlock(YES,YES,_indexPath);
    
}

/*
 左滑出现的删除按钮消失
*/
- (void)reumseCellAnimation:(BOOL)animation {
    
    UIButton *leftBtn = [self viewWithTag:333];
    
    if (animation) {
        
        [UIView animateWithDuration:0.3 animations:^{
            [self.bgView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(self).offset(15);
                make.right.equalTo(self).offset(-15);
            }];
            [self.deleteBtn mas_updateConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(self.mas_right).offset(-20);
            }];
            self.deleteBtn.alpha = 0;
            
            [leftBtn mas_updateConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(self.mas_left).mas_equalTo(25);
            }];
            
            [self layoutIfNeeded];
        }];
        
    } else {
        
        [self.bgView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(15);
            make.right.equalTo(self).offset(-15);
        }];
        [self.deleteBtn mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.mas_right).offset(-20);
        }];
        self.deleteBtn.alpha = 0;
        
        [leftBtn mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.mas_left).mas_equalTo(25);
        }];
    }
}

- (void)setIndexPath:(NSIndexPath *)indexPath {
    _indexPath = indexPath;
    
}

/*
 根据返回数据判断是否带有图片
 */
- (void)setNumtest:(NSInteger)numtest {
    _numtest = numtest;
 
    if (numtest%2==0) {
        /*无图片*/
        [_timeLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.bgView).offset(-10);
        }];
        CGFloat height = 10*6+13*2;
        [self addToView:self.deleteBtn Rect:CGRectMake(0,0,50,height) RounedCorner:UIRectCornerTopRight|UIRectCornerBottomRight withRadii:CGSizeMake(10, 10)];

        NSString *contentText = [NSString stringWithFormat:@"笔记内容:无图片"];
        _contentInfoLabel.attributedText = [self addAttribute:contentText WithSubstring:@"笔记内容:"];

        for (NSInteger i = 0; i<3; i++) {
            UIButton *imgBtn = [self viewWithTag:333+i];
            imgBtn.alpha = 0;
        }

    } else {
        /*有图片*/
        [_timeLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.bgView).offset(-10-10-W(40));
        }];
        CGFloat height = 10*6+13*2+10+W(40);
        [self addToView:self.deleteBtn Rect:CGRectMake(0,0,50,height) RounedCorner:UIRectCornerTopRight|UIRectCornerBottomRight withRadii:CGSizeMake(10, 10)];

        NSString *contentText = [NSString stringWithFormat:@"笔记内容:有图片"];
        _contentInfoLabel.attributedText = [self addAttribute:contentText WithSubstring:@"笔记内容:"];

        self.images = [NSMutableArray array];
        [self.images removeAllObjects];
        for (NSInteger i = 0; i<3; i++) {
            UIButton *imgBtn = [self viewWithTag:333+i];
            imgBtn.alpha = 1;
            [imgBtn setImage:[UIImage imageNamed:[NSString stringWithFormat:@"Guide_image%ld",i+1]] forState:UIControlStateNormal];
            imgBtn.contentMode = UIViewContentModeScaleAspectFill;
            [self.images addObject:[UIImage imageNamed:[NSString stringWithFormat:@"Guide_image%ld",i+1]]];
        }

    }

    NSString *fromText = [NSString stringWithFormat:@"笔记试题:《初中英语系统复习试卷》第%ld小题",numtest];
    _fromInfoLabel.attributedText = [self addAttribute:fromText WithSubstring:@"笔记试题:"];
    
    
}

- (NSAttributedString*)addAttribute:(NSString*)allString WithSubstring:(NSString*)subString {
    
    NSRange range = [allString rangeOfString:subString];
    NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:allString];
    [attr addAttribute:NSForegroundColorAttributeName value:rgba(153, 153, 153, 1) range:range];
    
    return attr;
}

#pragma mark - 图片浏览器
- (void)clickImage:(UIButton*)sender {
    
    XLPhotoBrowser *browser = [XLPhotoBrowser showPhotoBrowserWithCurrentImageIndex:sender.tag-333 imageCount:self.images.count datasource:self];
    browser.browserStyle = XLPhotoBrowserStyleIndexLabel;
    
}

- (UIImage *)photoBrowser:(XLPhotoBrowser *)browser placeholderImageForIndex:(NSInteger)index
{
    return self.images[index];
}

@end
